package com.kexio.pay.enums;

/**
 * 支付状态枚举
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public enum PaymentStatus {
    
    /**
     * 待支付
     */
    PENDING("PENDING", "待支付"),
    
    /**
     * 支付中 (已提交到第三方)
     */
    PROCESSING("PROCESSING", "支付中"),
    
    /**
     * 支付成功
     */
    SUCCESS("SUCCESS", "支付成功"),
    
    /**
     * 支付失败
     */
    FAILED("FAILED", "支付失败"),
    
    /**
     * 已取消
     */
    CANCELLED("CANCELLED", "已取消"),
    
    /**
     * 已关闭 (超时关闭)
     */
    CLOSED("CLOSED", "已关闭"),
    
    /**
     * 已退款
     */
    REFUNDED("REFUNDED", "已退款"),
    
    /**
     * 部分退款
     */
    PARTIAL_REFUNDED("PARTIAL_REFUNDED", "部分退款");
    
    private final String code;
    private final String description;
    
    PaymentStatus(String code, String description) {
        this.code = code;
        this.description = description;
    }
    
    public String getCode() {
        return code;
    }
    
    public String getDescription() {
        return description;
    }
    
    /**
     * 根据代码获取枚举
     */
    public static PaymentStatus fromCode(String code) {
        for (PaymentStatus status : values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        throw new IllegalArgumentException("Unknown payment status code: " + code);
    }
    
    /**
     * 是否为最终状态 (不会再变化)
     */
    public boolean isFinalStatus() {
        return this == SUCCESS || this == FAILED || this == CANCELLED || this == CLOSED || this == REFUNDED;
    }
    
    /**
     * 是否为成功状态
     */
    public boolean isSuccess() {
        return this == SUCCESS;
    }
    
    /**
     * 是否为失败状态
     */
    public boolean isFailure() {
        return this == FAILED || this == CANCELLED || this == CLOSED;
    }
    
    /**
     * 是否可以退款
     */
    public boolean canRefund() {
        return this == SUCCESS || this == PARTIAL_REFUNDED;
    }
    
    /**
     * 是否可以取消
     */
    public boolean canCancel() {
        return this == PENDING || this == PROCESSING;
    }
}
